package org.wyn.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.dao.impl.FreshThingDao;
import org.wyn.service.IFriendService;

import com.google.inject.Inject;

/**
 * 好友action
 * @author wyn
 *
 */
@Namespace("/friend")
@ParentPackage("security")
public class FriendAction extends WynActionSupport{
	@Action(value="goFriend",results={@Result(name=SUCCESS,type=ResultType.FREEMARKER,location="friend.ftl")})
	public String goFriend(){
		return SUCCESS;
	}
	@Action(value="goFriendSearch",results={@Result(name=SUCCESS,type=ResultType.FREEMARKER,location="friend_search.ftl")})
	public String goFriendSearch(){
		return SUCCESS;
	}
	/**
	 * 搜索用户
	 * @return
	 */
	@Action(value="friendSearch",results={@Result(name=SUCCESS,type=ResultType.FREEMARKER,location="friend_search_result.ftl")})
	public String friendSearch(){
		List<UserDetail> uds = iFriendService.searchUserByRealName(friendName);
		getRequest().setAttribute("uds", uds);
		return SUCCESS;
	}
	/**
	 * 发送好友请求
	 */
	@Action(value="become_friend_request",results={@Result(type=ResultType.PLAIN_TEXT)})
	public void becomeFriendRequest(){
		output(String.valueOf(iFriendService.becomeFriendRequest(getLoginUser().getUserName(), friendName)));
	}
	/**
	 * 查看我的好友请求(别人请求我)
	 * @return
	 */
	@Action(value="view_friend_request",results={@Result(name=SUCCESS,type=ResultType.FREEMARKER,location="view_friend_request.ftl")})
	public String viewFriendRequest(){
		getRequest().setAttribute("requestList",iFriendService.viewFriendRequestByToUser(getLoginUser().getUserName()));
		return SUCCESS;
	}
	/**
	 * 同意好友请求
	 */
	@Action(value="agree_request",results={@Result(type=ResultType.PLAIN_TEXT)})
	public void agreeRequest(){
		iFriendService.agreeRequest(getLoginUser().getUserName(),requestId);
	}
	/**
	 * 忽略(拒绝)好友请求
	 */
	@Action(value="reject_request",results={@Result(type=ResultType.PLAIN_TEXT)})
	public void rejectRequest(){
		iFriendService.rejectRequest(getLoginUser().getUserName(),requestId);
	}
	/**
	 * 查看我的好友列表
	 * @return
	 */
	@Action(value="friend_view",results={@Result(name=SUCCESS,type=ResultType.FREEMARKER,location="friend_view.ftl")})
	public String friendView(){
		List<UserDetail> list = iFriendService.viewFriendByUserName(getLoginUser().getUserName());
		getRequest().setAttribute("friendUds", list);
		return SUCCESS;
	}
	private String requestId;
	private String friendName;
	private static final long serialVersionUID = 1L;
	@Inject
	private IFriendService iFriendService;
	public String getFriendName() {
		return friendName;
	}
	public void setFriendName(String friendName) {
		this.friendName = friendName;
	}
	public IFriendService getiFriendService() {
		return iFriendService;
	}
	public void setiFriendService(IFriendService iFriendService) {
		this.iFriendService = iFriendService;
	}
	public String getRequestId() {
		return requestId;
	}
	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}
}
